<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th><input type="checkbox" v-model="all" /> <span>全选</span></th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(val, index) in arr" :key="index">
          <td><input type="checkbox" v-model="val.checked" /></td>
          <td>{{ val.name }}</td>
          <td>{{ val.price }}</td>
          <td>
            <button @click="jian(index)">-</button>
            {{ val.num }}
            <button @click="jia(index)">+</button>
          </td>
          <td>{{ val.price * val.num }}</td>
          <td>
            <button @click="del(index)">删除</button>
          </td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>合计:</td>
          <td colspan="5">{{ sum }}</td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
export default {
  props: ["arr"],
  methods: {
    del(index) {
      this.$emit("delList", index);
    },
    jia(index) {
      this.$emit("jia", index);
    },
    jian(index) {
      this.$emit("jian", index);
    },
  },
  computed: {
    sum() {
      return this.arr.reduce((s, i) => {
        return s + i.price * i.num;
      }, 0);
    },
    all: {
      set(val) {
        this.$emit("show", val);
      },
      get() {
        return this.arr.every((obj) => {
          return obj.checked == true;
        });
      },
    },
  },
};
</script>

<style>
</style>